<?php
/**
 * Created by Benjamin.
 * Date: 2020/5/6
 * Time: 15:03
 */

namespace App\HttpController;
use App\HttpController\AbstractInterface\REST;
use App\Model\SmsModel;
use EasySwoole\Http\Message\Status;
use EasySwoole\Http\Request;
use EasySwoole\Validate\Validate;

class Sms extends REST
{
    /**
     * 发验证码
     *  @Method(allow={POST})
     *
     */
    function POSTSms(){
        $param =  $this->request()->getRequestParam();
        $valitor = new Validate();
        $valitor->addColumn('mobile', '手机号不为空')->required('手机号不为空')->numeric('手机号只能为数字')->lengthMax(11,'手机号最长11位');
        $valitor->addColumn('prefix','区号')->required('区号不能为空')->lengthMin(1,'区号长度错误');

        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(Status::CODE_BAD_REQUEST, '', $valitor->getError()->__toString());
        }

        $instance = \EasySwoole\EasySwoole\Config::getInstance();
        // 获取配置 按层级用点号分隔
        $limit = $instance->getConf('SMS_CODE_LIMIT');

        $model = new SmsModel($param);
        $model->create_time = time();
        $model->update_time = time();
        $model->content = mt_rand(1000,9999);
        $rs = $model->save();
        if ($rs) {
            //进短信队列
            $this->writeJson(Status::CODE_OK, $rs, "发送成功". $model->content);
        } else {
            $this->writeJson(Status::CODE_BAD_REQUEST, [], $model->lastQueryResult()->getLastError());
        }
    }


}